package com.supreme.basicserver.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.supreme.basicserver.mapper.mybatis.MBUserMapper;
import com.supreme.commonentity.mysql.primary.SysUser;
import com.supreme.commonboot.utils.AjaxResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "user")
public class UserController {

    private final MBUserMapper mbUserMapper;

    @Autowired
    public UserController(MBUserMapper mbUserMapper) {
        this.mbUserMapper = mbUserMapper;
    }

    @GetMapping(value = "login")
    public AjaxResp userLogin(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);

        SysUser selectOne = this.mbUserMapper.selectOne(queryWrapper);
        if (selectOne != null) {
            StpUtil.login(selectOne.getUserId());
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            return new AjaxResp(tokenInfo);
        }

        return new AjaxResp(1, "error");
    }

    @GetMapping(value = "search")
    public AjaxResp searchUser() {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        List<SysUser> userList = this.mbUserMapper.selectList(queryWrapper);
        return new AjaxResp(userList);
    }
}
